package com.daydayup.stream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamTest4 {
    public static void main(String[] args) {

        List<Student> students = new ArrayList<>();
        Student stu;
        for(int i = 0;i<10;i++){
            stu = new Student();
            stu.setName("张"+i);
            stu.setAge(i+20);
            students.add(stu);
        }
        for(int i = 0;i<10;i+=2){
            stu = new Student();
            stu.setName("张"+i);
            stu.setAge(i);
            students.add(stu);
        }

        Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(e -> e.getName()));
        System.out.println(map);

//        students.stream().collect(Collectors.groupingBy(e -> e.getName(),))

        Map<String, List<Student>> map1 = new HashMap<>();

        for (Student student : students) {
            List<Student> students1 = map1.get(student.getName());
            if(student == null){
                students1 = new ArrayList<>();
                students1.add(student);
                map1.put(student.getName(), students1);
            }else{
                students1.add(student);
            }
        }

    }
}
